﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using SmartTravelAssistant.Models;
using System.Collections;

namespace SmartTravelAssistant.Controllers
{
    public class TripController : Controller
    {
        TrasContext context = new TrasContext(WebConfigurationManager.AppSettings["BrightstarDB.ConnectionString"]);
        //
        // GET: /Trip/

        public ActionResult Index()
        {
            return View();
        }

        
        [HttpPost]
        [Authorize]
        public JsonResult AddPlaceToTrip(InterestPoint place)
        {
            try
            {
                Person currentUser = (Person)this.Session["currentprofile"];
                Person user = (Person)context.Persons.Where(x => x.Name == currentUser.Name).FirstOrDefault();
                context.InterestPoints.Add(place);

                if (user.CurrentTrip == null)
                {
                    Trip trip = new Trip();
                    trip.Author = user;
                    trip.StartDay = DateTime.Now;
                    trip.Photo = place.Pictures;
                  

                    trip.InterestPoints.Add(place);
                    context.Trips.Add(trip);
                    user.CurrentTrip = trip;

                    context.SaveChanges();
                    return Json(new { Succes = "Added to trip Succesfully!" }, JsonRequestBehavior.DenyGet);
                }
                else
                {

                    user.CurrentTrip.Author = user;
                    user.CurrentTrip.StartDay = DateTime.Now;
                    user.CurrentTrip.Photo = place.Pictures;

                    user.CurrentTrip.InterestPoints.Add(place);
                    context.SaveChanges();
                    return Json(new { Succes = "Added to trip Succesfully!" }, JsonRequestBehavior.DenyGet);
                }
            }catch(Exception e){

            return Json(new { Error = "An error occurred!"}, JsonRequestBehavior.DenyGet);
            }
        }

        [Authorize]
        public JsonResult GetCurrentTrip()
        {
            Person currentUser = (Person)this.Session["currentprofile"];
            Person user = (Person)context.Persons.Where(x => x.Name == currentUser.Name).FirstOrDefault();
            Trip returnTrip = new Trip();
            if (user.CurrentTrip != null)
            {
                returnTrip = new Trip();
                foreach (InterestPoint item in user.CurrentTrip.InterestPoints)
                {
                    InterestPoint i = new InterestPoint();
                    i.Icon = item.Icon;
                    i.Name = item.Name;
                    i.Pictures = item.Pictures;
                    i.Reference = item.Reference;
                    i.Lat = item.Lat;
                    i.Long = item.Long;
                    returnTrip.InterestPoints.Add(i);
                }
            }
            else
            {
                returnTrip = new Trip();
            }

            return Json(returnTrip, JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        public JsonResult SaveCurrentTrip(Trip trip)
        {
            Person currentUser = (Person)this.Session["currentprofile"];
            Person user = (Person)context.Persons.Where(x => x.Name == currentUser.Name).FirstOrDefault();

            user.CurrentTrip.Name = trip.Name;
            user.CurrentTrip.Description = trip.Description;

            user.CreatedTrips.Add(user.CurrentTrip);

            user.CurrentTrip = null;

            context.SaveChanges();

            return Json(new { Success = "Trip saved successfully!"}, JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        [HttpPost]
        public JsonResult GetUserTrips()
        {
            Person currentUser = (Person)this.Session["currentprofile"];
            Person user = (Person)context.Persons.Where(x => x.Name == currentUser.Name).FirstOrDefault();
            //Trip returnTrip = new Trip();
            List<Trip> trips = new List<Trip>();
            foreach (Trip item in user.CreatedTrips)
            {
                Trip trip = new Trip();
                trip.Id = item.Id;
                trip.Name = item.Name;
                trip.Description = item.Description;
                trip.StartDay = item.StartDay;
                //trip.Author = item.Author;
                trips.Add(trip);
            }
            return Json(trips, JsonRequestBehavior.DenyGet);
        }

        [Authorize]
        [HttpPost]
        public JsonResult AttendTrip(string param)
        {
            Person currentUser = (Person)this.Session["currentprofile"];
            Person user = (Person)context.Persons.Where(x => x.Name == currentUser.Name).FirstOrDefault();

            Trip t = (Trip)context.Trips.Where(x => x.Name == param).FirstOrDefault();
            user.AttendedTrips.Add(t);
            t.Attendees.Add(user);
            context.SaveChanges();
            return Json(new { Success = "Success!"}, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult getTripAttenders(string param)
        {
            
            Trip t = (Trip)context.Trips.Where(x => x.Name == param).FirstOrDefault();

            List<Person> returnList = new List<Person>();
            foreach (Person p in t.Attendees)
            {
                Person pe = new Person() { Name = p.Name};
                returnList.Add(pe);
            }

            return Json(returnList, JsonRequestBehavior.DenyGet);
        }


        [HttpPost]
        public JsonResult getGalleryInfo(string param)
        {
            Person currentUser = (Person)this.Session["currentprofile"];
            Person user = (Person)context.Persons.Where(x => x.Name == currentUser.Name).FirstOrDefault();

            Trip t = (Trip)context.Trips.Where(x => x.Name == param).FirstOrDefault();

            var Email = t.Author.Email;

            return Json(Email, JsonRequestBehavior.DenyGet);
        }
        
        public JsonResult GetATrip(String param)
        {
            Trip trip = (Trip)context.Trips.Where(x => x.Name == param).FirstOrDefault();
    
             
            Trip returnTrip = new Trip();
            if (trip != null)
            {
                returnTrip = new Trip();
                foreach (InterestPoint item in trip.InterestPoints)
                {
                    InterestPoint i = new InterestPoint();
                    i.Icon = item.Icon;
                    i.Name = item.Name;
                    i.Pictures = item.Pictures;
                    i.Reference = item.Reference;
                    i.Lat = item.Lat;
                    i.Long = item.Long;
                    returnTrip.InterestPoints.Add(i);
                }
                returnTrip.Name = trip.Name;
                returnTrip.Description = trip.Description;
                returnTrip.StartDay = trip.StartDay;
                returnTrip.Author = new Person() { Name = trip.Author.Name };
                //returnTrip.Author = trip.Author;
            }
            else
            {
                returnTrip = new Trip();
            }

            return Json(returnTrip, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult GetUserTripsRecomandations()
        {
            Person currentUser = (Person)this.Session["currentprofile"];
            Person user = (Person)context.Persons.Where(x => x.Name == currentUser.Name).FirstOrDefault();
            
            List<Trip> recomendedTrips = new List<Trip>();
            foreach (Person friend in user.Colleagues)
            {
                foreach (Trip item in friend.CreatedTrips)
                {
                    Trip trip = new Trip();
                    trip.Id = item.Id;
                    trip.Name = item.Name;
                    trip.Description = item.Description;
                    trip.StartDay = item.StartDay;
                    trip.Photo = item.Photo;
                    recomendedTrips.Add(trip);
                }
                foreach (Trip item in friend.AttendedTrips)
                {
                    Trip trip = new Trip();
                    trip.Id = item.Id;
                    trip.Name = item.Name;
                    trip.Description = item.Description;
                    trip.StartDay = item.StartDay;
                    trip.Photo = item.Photo;
                    recomendedTrips.Add(trip);
                }
            }
            return Json(recomendedTrips, JsonRequestBehavior.DenyGet);
        }


        //
        // GET: /Trip/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Trip/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Trip/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Trip/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Trip/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Trip/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Trip/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
